package cn.pfz.controller;

import cn.pfz.Service.IEmployeeSerives;
import cn.pfz.Service.IPermissionSerives;

import cn.pfz.Service.IRolePermissionSerives;
import cn.pfz.annotation.RequiredPermission;
import cn.pfz.domain.Department;
import cn.pfz.domain.Employee;
import cn.pfz.domain.JsonResult;
import cn.pfz.domain.Permission;
import cn.pfz.query.EmployeeQuery;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionSerives iPermissionSerives;

    @Autowired
    private ApplicationContext ctx;

    @Autowired
    private IRolePermissionSerives rolePermissionSerives;

    @Autowired
    private IEmployeeSerives employeeSerives;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        PageInfo<Permission> list = iPermissionSerives.selectRusult(qo);
        model.addAttribute("result",list);
        return "permission/list";
    }

    @RequestMapping ("/reload")
    @ResponseBody
    public JsonResult reload(HttpServletRequest session){
        String currentuser = (String) session.getSession().getAttribute("currentuser");
        String currentuserpassword = (String) session.getSession().getAttribute("currentuserpassword");

        Employee employee = employeeSerives.selectByUsername(currentuser);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(employee.getUsername(), currentuserpassword);
        SecurityUtils.getSubject().login(usernamePasswordToken);

        session.getSession().setAttribute("currentuser", employee.getUsername());

//获取数据库所有权限
        List<Permission> permissions = iPermissionSerives.selectAll();
        HashMap<String,String> hashMap = new HashMap();
        for (Permission permission : permissions) {
            hashMap.put(permission.getName(),permission.getExpression());
        }
        Map<String, Object> beansWithAnnotation = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> values = beansWithAnnotation.values();
        for (Object value : values) {
            Class<?> aClass = value.getClass();
            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                RequiredPermission annotation = declaredMethod.getAnnotation(RequiredPermission.class);
                if(annotation!=null){
                    //找到贴权限的方法
                    String name = annotation.name();
                    String experssion = annotation.experssion();
                    if(hashMap.get(name)==null){
                        iPermissionSerives.insert(new Permission(null,name,experssion));
                    }
                }
            }
        }
        //刷新当前登录用户信息
        return JsonResult.success(null);
    }



    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission department){
        if(department.getId()!=null){
            //修改
            iPermissionSerives.updateByPrimaryKey(department);
        }else{
            iPermissionSerives.insert(department);
        }
        return "redirect:/permission/list";
    }

    @ResponseBody
    @RequestMapping ("/delete")
    public JsonResult delete(Long id){
        iPermissionSerives.deleteByPrimaryKey(id);
        rolePermissionSerives.deleteByPermission_permission(id);

        return JsonResult.success(null);
    }



}
